package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {


    @Reference
    OrderSettingService orderSettingService;

    /**
     * 批量预约设置的上传文件方法
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(@RequestBody MultipartFile excelFile){
        try {
            List<String[]> stringsList = POIUtils.readExcel(excelFile);
            System.out.println(stringsList);
            //把string数组的list集合转换为 OrderSetting对象的list集合
            //创建OrderSetting的list集合对象
            List<OrderSetting> orderSettingList = new ArrayList<>();
            for (String[] strings : stringsList) {
                //一个string数组对应一个OrderSetting对象
                OrderSetting orderSetting = new OrderSetting();
                //获取数组的第0个元素
                String orderDateStr = strings[0];
                //把字符串日期转换为Date格式
                SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
                Date orderDate  = format.parse(orderDateStr);
                //预约时间
                orderSetting.setOrderDate(orderDate);
                // 已预约人数
                orderSetting.setReservations(0);
                //获取数组第一个元素
                String numberStr = strings[1];
                //可预约人数
                orderSetting.setNumber(Integer.parseInt(numberStr));
                //把预约对象添加到集合中
                orderSettingList.add(orderSetting);
            }
            orderSettingService.addOrderSettingList(orderSettingList);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("/findByMonth")
    public Result findByMonth(String month){
        try {
            List<OrderSetting> orderSettingList = orderSettingService.findByMonth(month);
            /**
             * 返回的数据结构
             * [
             *  {
             *      orderDate: 2019/6/1,
             *      number:200,
             *      reservations ：100
             *  },
             *  {
             *      orderDate: 2019/6/1,
             *      number:200,
             *      reservations ：100
             *  },
             * ]
             *
             * 需要的数据结构
             *  [
             //     { date: 1, number: 120, reservations: 1 },
             //     { date: 3, number: 120, reservations: 1 },
             //     { date: 4, number: 120, reservations: 120 },
             //     { date: 6, number: 120, reservations: 1 },
             //     { date: 8, number: 120, reservations: 1 }
             // ];
             *
             */
            //把查询出来的数据转换为页面需要的数据结构
            List<Map<String,Object>> mapList = new ArrayList<>();
            for (OrderSetting orderSetting : orderSettingList) {
                //一个orderSetting对应一个Map
                Map<String,Object> map = new HashMap<>();
                Date orderDate = orderSetting.getOrderDate();
                map.put("date", orderDate.getDate())   ;
                map.put("number",orderSetting.getNumber() )   ;
                map.put("reservations", orderSetting.getReservations());
                //把map对象添加到集合中
                mapList.add(map);
            }
            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS, mapList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }


    @RequestMapping("/addOrEdit")
    public Result addOrEdit(@RequestBody OrderSetting orderSetting){
        try {
            List<OrderSetting> orderSettingList = new ArrayList<>();
            orderSettingList.add(orderSetting);
            orderSettingService.addOrderSettingList(orderSettingList);
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }
}
